package com.lh.api.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.lh.api.dao.ActivityShopOneMapper;
import com.lh.api.dao.ShopCartMapper;
import com.lh.api.dao.ShopMapper;
import com.lh.api.service.ShopService;
import com.lh.api.vo.OneDoboVo;
import com.lh.api.vo.PropertyVo;
import com.lh.api.vo.SearchVo;
import com.lh.api.vo.ShopDetailVo;
import com.lh.common.entity.admin.Shop;
import com.lh.common.enums.ShopSortTypeEnum;
import com.lh.common.enums.ShopTypeEnum;
import com.lh.common.utils.AssertUtil;
import com.lh.common.utils.Constant;
import com.lh.common.utils.PageUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author Gze
 * @ClassName: ShopServiceImpl
 * @Description: (类作用)
 * @date: 2020-05-14 10:57
 */
@Service
public class ShopServiceImpl extends ServiceImpl<ShopMapper, Shop> implements ShopService {

    @Autowired
    private ActivityShopOneMapper activityShopOneMapper;

    @Autowired
    private ShopCartMapper shopCartMapper;

    @Override
    public PageUtils<SearchVo> searchObj(Map<String, Object> params, Integer pageNum, Integer pageSize) {
        if(pageNum != null && pageSize != null){
            PageHelper.startPage(pageNum, pageSize);
        }
        List<SearchVo> searchVos = this.baseMapper.searchObj(params);
        addData(searchVos);
        return new PageUtils<>(searchVos);
    }

    @Override
    public PageUtils<SearchVo> searchObj(Map<String, Object> params, ShopSortTypeEnum sortType, Integer pageNum, Integer pageSize) {
        if(pageNum != null && pageSize != null){
            PageHelper.startPage(pageNum, pageSize);
        }
        String sortName = "create_time";
        String sortOrder = "DESC";
        if (ShopSortTypeEnum.PRICEDESC.equals(sortType)){
            sortName = "price";
            sortOrder = "DESC";
        }else if (ShopSortTypeEnum.PRICEASC.equals(sortType)){
            sortName = "price";
            sortOrder = "ASC";
        }
        params.put("sortName", sortName);
        params.put("sortOrder", sortOrder);
//        List<SearchVo> searchVos = this.baseMapper.searchObj(params);
        List<SearchVo> searchVos = this.baseMapper.searchSortObj(params);
        addData(searchVos);
        return new PageUtils<>(searchVos);
    }

    private void addData(List<SearchVo> list) {
        list.forEach(searchVo -> {
            searchVo.setPercent(new BigDecimal(0));
            if(searchVo.getPortionNum() > 0 ){
                searchVo.setPercent(new BigDecimal(searchVo.getCurrenPortion() * 100).divide(new BigDecimal(searchVo.getPortionNum()), 2, BigDecimal.ROUND_HALF_UP));
            }
        });
    }

    @Override
    public List<SearchVo> getIndexList() {
        List<SearchVo> searchVos = this.baseMapper.getIndexList();
        addData(searchVos);
        return searchVos;
    }

    @Override
    public PageUtils<SearchVo> getlist(String  qureyTime,ShopTypeEnum shopType, Integer pageNum, Integer pageSize) {
        if(pageNum != null && pageSize != null){
            PageHelper.startPage(pageNum, pageSize);
        }
        List<SearchVo> searchVos = null;
        if(ShopTypeEnum.SHOP.equals(shopType)){
            searchVos = this.baseMapper.getlist(qureyTime);
        }else {
            searchVos = activityShopOneMapper.getlist(qureyTime);
        }

        addData(searchVos);
        return new PageUtils<>(searchVos);
    }
    @Override
    public PageUtils<SearchVo> getlist(String  qureyTime, ShopSortTypeEnum sortType, ShopTypeEnum shopType, Integer pageNum, Integer pageSize) {
        if(pageNum != null && pageSize != null){
            PageHelper.startPage(pageNum, pageSize);
        }
        String sortName = "create_time";
        String sortOrder = "DESC";
        if (ShopSortTypeEnum.PRICEDESC.equals(sortType)){
            sortName = "price";
            sortOrder = "DESC";
        }else if (ShopSortTypeEnum.PRICEASC.equals(sortType)){
            sortName = "price";
            sortOrder = "ASC";
        }

        List<SearchVo> searchVos = null;
        if(ShopTypeEnum.SHOP.equals(shopType)){
            searchVos = this.baseMapper.getSortList(qureyTime,sortName,sortOrder);
        }else {
            searchVos = activityShopOneMapper.getlist(qureyTime);
        }

        addData(searchVos);
        return new PageUtils<>(searchVos);
    }
    @Override
    public PageUtils<SearchVo> getRecommendShopList(String qureyTime, Integer pageNum, Integer pageSize){
        if(pageNum != null && pageSize != null){
            PageHelper.startPage(pageNum, pageSize);
        }
        List<SearchVo> searchVos = this.baseMapper.getRecommendShopList(qureyTime);
        addData(searchVos);
        return new PageUtils<>(searchVos);
    }

    @Override
    public PageUtils<OneDoboVo> oneDoboList(String qureyTime, Integer pageNum, Integer pageSize) {
        if(pageNum != null && pageSize != null){
            PageHelper.startPage(pageNum, pageSize);
        }

        List<OneDoboVo> oneDoboList = this.baseMapper.oneDoboList(qureyTime);
        for (OneDoboVo oneDoboVo : oneDoboList){
            if(oneDoboVo.getPortionNum() > 0 ){
                oneDoboVo.setPercent(new BigDecimal(oneDoboVo.getCurrenPortion() * 100).divide(new BigDecimal(oneDoboVo.getPortionNum()), 2, BigDecimal.ROUND_HALF_UP));
            }
        }
        return new PageUtils<>(oneDoboList);
    }

    @Override
    public PageUtils<OneDoboVo> oneDoboList(String qureyTime, ShopSortTypeEnum sortType, Integer pageNum, Integer pageSize) {
        if(pageNum != null && pageSize != null){
            PageHelper.startPage(pageNum, pageSize);
        }

        String sortName = "create_time";
        String sortOrder = "DESC";
        if (ShopSortTypeEnum.PRICEDESC.equals(sortType)){
            sortName = "shop_activity_price";
            sortOrder = "DESC";
        }else if (ShopSortTypeEnum.PRICEASC.equals(sortType)){
            sortName = "shop_activity_price";
            sortOrder = "ASC";
        }

//        List<OneDoboVo> oneDoboList = this.baseMapper.oneDoboList(qureyTime);
        List<OneDoboVo> oneDoboList = this.baseMapper.oneDoboSortList(qureyTime,sortName,sortOrder);
        for (OneDoboVo oneDoboVo : oneDoboList){
            if(oneDoboVo.getPortionNum() > 0 ){
                oneDoboVo.setPercent(new BigDecimal(oneDoboVo.getCurrenPortion() * 100).divide(new BigDecimal(oneDoboVo.getPortionNum()), 2, BigDecimal.ROUND_HALF_UP));
            }
        }
        return new PageUtils<>(oneDoboList);
    }

    @Override
    public ShopDetailVo getShopDetail(Long sid, Long userId) {
        ShopDetailVo result = baseMapper.getShopDetail(sid);
        AssertUtil.assertNotNull(result, Constant.RESULT.FI1000,"Item has been removed!");

        if(StringUtils.isNotEmpty(result.getProperty())){
            result.setPropertyList(parseData(result.getProperty()));
        }

        if(StringUtils.isNotEmpty(result.getImg())){
            result.setImgList(Arrays.asList(result.getImg().split(",")));
        }

        if(userId != null){
            Integer shopCartNum = shopCartMapper.getShopCartNum(userId);
            result.setShopCartNum(shopCartNum);
        }

        return result;
    }

    public static List<PropertyVo> parseData(String property) {
        JSONArray jsonArray = JSONArray.fromObject(property);
        List<PropertyVo> propertyVoList = new ArrayList<>();
        PropertyVo temp = null;
        for(int i=0;i<jsonArray.size();i++){
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            temp = new PropertyVo();
            temp.setTitle(jsonObject.get("title") == null ? "" : jsonObject.get("title").toString());
            temp.setValue(jsonObject.get("value") == null ? "" : jsonObject.get("value").toString());
            propertyVoList.add(temp);
        }
        return propertyVoList;
    }
}
